<template>
<div class="main">
  <div class="heard">
    <div class="nav"><a></a>
    </div>
    <h2 class="pag_title">修改收货地址</h2>
  </div>
  <van-address-edit
  :area-list="areaList"
  show-postal
  show-delete
  show-set-default
  show-search-result
  :search-result="searchResult"
  :area-columns-placeholder="['请选择', '请选择', '请选择']"
  @save="onSave"
  @delete="onDelete"
  
  :address-info="addressInfo"
/>

</div>
 
</template>

<script>
  import { Toast } from 'vant';
   import { areaList } from '@vant/area-data';
import axios from "axios"
import {base_url} from '../utils/baseURL'
// import axios from 'axios';
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      addressInfo:{
                id:'',
                name:'',
                tel:'',
                province:'',
                city:'',
                county:'',
                addressDetail:'',
                areaCode:'',
                postalCode:'',
                isDefault:'',
                address:'',
            }
    };
  },
created () {
  let item = sessionStorage.getItem("userObj")
  let idx=JSON.parse(item).id
    let {id}=this.$route.params;
    axios.get(`${base_url}userdata`).then(res=>{
       this.addressInfo=res.data[idx-1].addressInfos[id]
      console.log(res.data[idx-1].addressInfos[id])
    })
   
},
  methods: {
    onSave(content) {
        let item = sessionStorage.getItem("userObj")
  let idx=JSON.parse(item).id
    let {id}=this.$route.params;
    axios.get(`${base_url}userdata`).then(res=>{
      console.log(content)
       
       if(content.isDefault==true){
         console.log(111)
         for(var i=0;i<=res.data[idx-1].addressInfos.length-1;i++){
           res.data[idx-1].addressInfos[i].isDefault=false
          //  console.log(res.data[idx-1].addressInfos[i].isDefault)
         }
       }
       res.data[idx-1].addressInfos[id]=content

      console.log(res.data[idx-1].addressInfos)
      let url=`${base_url}userdata/${idx}`;
      axios.put(url,res.data[idx-1]) 
    })
         this.$router.push('/checkouts')
        },
        
    onDelete() {
     let item = sessionStorage.getItem("userObj")
      let idx=JSON.parse(item).id
    let {id}=this.$route.params;
    axios.get(`${base_url}userdata`).then(res=>{
      console.log(res.data[idx-1].addressInfos)
     let red=res.data[idx-1].addressInfos.splice(id,1)
      console.log(res.data[idx-1].addressInfos)
     
      let url=`${base_url}userdata/${idx}`;
      axios.put(url,res.data[idx-1]) 
    })
     this.$router.push('/checkout')
    },
    
  },
  computed: {
      addressList() {
      return this.$store.state.address.addressList;
    },
  },
};

</script>

<style lang="less" scoped>
@import url('../assets/css/addtheaddress.css');



</style>